$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'pro/sickdetail/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id_', width: 45, key: true,hidden:true },
            { label: '所属组织', name: 'deptName', width: 75 },
			{ label: '床位号', name: 'bedNO', width: 50 },
			{ label: '姓名', name: 'sickName', width: 50 },
//			{ label: '患者编号', name: 'IDNO', width: 50 },
			{ label: '身份证号码', name: 'ICNO', width: 50 },
			{ label: '性别', name: 'sex', width: 40 , formatter: function(value, options, row){
				return value == 1 ? 
						'<span>女</span>' : 
						'<span>男</span>';
			}},
			{ label: '住院号', name: 'inHosNO', width: 50 ,
				formatter:function(value,options,row){
						return '<span class="label label-primary">'+value+'</span>';
					}},
			{ label: '年龄', name: 'sickAge', width: 40 },
			{ label: '入院时间', name: 'inHosTime', width: 50 },
			{ label: '出院时间', name: 'outHosTime', width: 50 },
//			{ label: '入院天数', name: 'inHostDay', width: 50 },
			{ label: '管床护士', name: 'mgBedNurse', width: 50 },
			{ label: '护理等级', name: 'nurseLevel', width: 50, 
				formatter:function(value,options,row){
					if(value==1){
						return '<span>I级</span>';
					}else if(value==2){
						return '<span>II级</span>';
					}else{
						return '<span>III级</span>';
					}
					}},
			{ label: '医保类型', name: 'medicalType', width: 50,
				formatter:function(value,options,row){
				if(value==1){
					return '<span>城镇职工基本医疗保险</span>';
				}else if(value==2){
					return '<span>城镇居民基本医疗保险</span>';
				}else if(value==3){
					return '<span>新型农村合作医疗</span>';
				}else{
					return '<span>自费</span>';
				}
				}},
			{ label: '住院预交金（总金额）', name: 'amountOfMoney', width: 50 },
			{ label: '已使用金额', name: 'spendMoney', width: 50 },
			{ label: '可以使用的额度', name: 'limitSpendMoney', width: 50 },
			{ label: '余额', name: 'balance', width: 50 ,
				formatter:function(value,options,row){
					return '<span class="label label-warning">'+value+'</span>';
				}},
			{ label: '类型', name: 'sickIllType', width: 50,
				formatter:function(value,options,row){
					if(value==1){
						return '<span>单病种</span>';
					}else if(value==2){
						return '<span>临床路径</span>';
					}else{
						return '<span>会诊信息</span>';
					}
					}},
			{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
				return value === 0 ? 
						'<span class="label label-danger">禁用</span>' : 
						'<span class="label label-success">正常</span>';
			}},
        	{ label: '时间', name: 'updateTime', width: 90 }
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};

var settingSick = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "deptId",
//	            pIdKey: "null",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    }
	};
var ztree;
var ztreeSick;

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			sickName: null
		},
		showList: true,
		title:null,
		sickdetail:{
			
		},
	    bedNOList:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.queryBedNOList();
			vm.showList = false;
			vm.title = "新增";
			vm.roleList = {};
			vm.sickdetail = {deptName:null, deptId:null, status:1,sickAreaNO:null,sickAreaName:null};

			vm.getDept();
			   $("#bedNO").val("-1");
				
		},
        getDept: function(){
            //加载组织树
            $.get(baseURL + "sys/dept/list", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.sickdetail.deptId);
                if(node != null){
                    ztree.selectNode(node);
                    vm.sickdetail.deptName = node.name;
				}
            })
        },
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.queryBedNOList();
			vm.showList = false;
            vm.title = "修改";
			
			vm.getSickdetail(userId);
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pro/sickdetail/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.sickdetail.id_ == null ? "pro/sickdetail/save" : "pro/sickdetail/update";
			vm.sickdetail.inHosTime =$("#inHosTime").val();
			vm.sickdetail.outHosTime =$("#outHosTime").val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.sickdetail),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getSickdetail: function(userId){
			$.get(baseURL + "pro/sickdetail/info/"+userId, function(r){
				vm.sickdetail = r.sickdetail;
				//vm.dctnrs.passWord = null;

                vm.getDept();
			});
		},
		queryBedNOList: function () {//查询床号List
			$.ajax({
				type: "POST",
			    url: baseURL + "sys/remind/queryBedNOList",
                contentType: "application/json",
			    data: JSON.stringify(vm.sickdetail),
			    success: function(r){
					if(r.code == 0){
						vm.bedNOList=r.bedNOList;
						$("#bedNO").val(r.bedNOList);
					}else{
						//alert(r.msg);
					}
				}
			});
		},
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择组织",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级组织
                    vm.sickdetail.deptId = node[0].deptId;
                    vm.sickdetail.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
        deptTreeSick: function(){
        	if(vm.sickdetail.deptId==null){
        		alert("请先选择组织");
        		return;
        	}
        	vm.getDeptSickUpdate();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择病区",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayerSick"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztreeSick.getSelectedNodes();
                    //选择上级组织
                    if(node.length>0){
                    	  vm.sickdetail.sickAreaNO = node[0].deptId;
                          vm.sickdetail.sickAreaName = node[0].name;
                    }
                      
                    layer.close(index);
                }
            });
        },
        getDeptSickUpdate: function(){
        	if(vm.sickdetail.deptId==null){
        		alert("请先选择组织");
        		return;
        	}
        	//加载组织树
        	$.get(baseURL + "sys/dept/listSick/"+vm.sickdetail.deptId+"", function(r){
        		ztreeSick = $.fn.zTree.init($("#deptTreeSick"), settingSick, r);
        		var node = ztreeSick.getNodeByParam("deptId", vm.sickdetail.sickAreaNO);
        		if(node!=null){
        			ztreeSick.selectNode(node);
        			vm.sickdetail.sickAreaNO=node.deptId;
        			vm.sickdetail.sickAreaName=node.name;
        			
        		}
        		
        	})
        },
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'sickName': vm.q.sickName},
                page:page
            }).trigger("reloadGrid");
		}
	}
	
});

